#!/usr/bin/python
# -*- coding:utf-8 -*-

import MySQLdb


def insert_off_shelve(two_sold):

    db = MySQLdb.connect("127.0.0.1", "root", "123", "openDB")
    cursor = db.cursor()
    with open("./twoweeks_sold.csv", "r") as f:
        items = f.readlines()
    for item in items:
        item_id = item.split('\t')[0]
        sold = item.split('\t')[1].strip()
        try:
            sql = "insert into " + two_sold + "(product_id, sold, date_add) values ((select id from product_1 where product_id = " + item_id + "), "+ sold + ", now())"
            # print sql
            cursor.execute(sql)
            db.commit()
        except Exception, e:
            print str(e)
            db.rollback()
            continue
    db.close()


if __name__ == '__main__':
    insert_off_shelve('sold_1')
